package com.bnzj.core.json;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import com.bnzj.core.util.DateUtils;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

/**
 * 长日期与短日期都可以正常转换为Date
 * 
 * @author Administrator
 *
 */
public class DateDeserializer extends JsonDeserializer<Date> {

	@Override
	public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		try {
			return parseDate(p.getValueAsString());
		} catch (ParseException e) {
			throw new IOException(e);
		}
	}

	public static Date parseDate(String date) throws ParseException {
		if (StringUtils.isEmpty(date)) {
			return null;
		}
		return DateUtils.parseDate(date);
	}
}
